;(function () {
	'use strict';
	
	gm.system.init();
	
	/* This will serve as a wrapper around all SDK functionality */
	
	window.sdk = {};
	sdk.comm = {};
	sdk.gps = {};
	sdk.info = {};
	sdk.io = {};
	sdk.media = {};
	sdk.nav = {};
	sdk.system = {};
	sdk.ui = {};
	
	var screenSize = 8;
	//TOUPDATE
	var region = "North America";
	
	var media = {};
	
	sdk.comm.getNetworkConnectivity = function (callback) {
		return gm.comm.getNetworkConnectivity(function (status) {
    		callback(status);
    	}, function () {
    		callback(false);
    	});
	};
	
	sdk.comm.watchNetworkConnectivity = function (callback) {
		return gm.comm.watchNetworkConnectivity(function (status) {
			log("Inside watchNetworkConnectivity, status: " + status);
			/*log("Inside watchNetworkConnectivity, status: " + status + ", media status: " + media.status);
			if (!status && (media.status == "playing" || media.status == "buffering" || media.status == "connecting")) {
				sdk.media.pause();
			}
			else if (status && media.status == "paused") {
				sdk.media.pause();
			}*/
    		callback(status);
    	}, function () {
    		callback(false);
    	});
	};
	
	sdk.gps.getCurrentPosition = function (success, failure) {
		gm.info.getCurrentPosition(function (pos) {
			var position = {
				latitude: (pos.coords) ? pos.coords.latitude / 3600000 : 0,
				longitude: (pos.coords) ? pos.coords.longitude / 3600000 : 0
			};
			log("Got current position: " + JSON.stringify(position));
			success(position);
		}, function () {
			log("Failed to get GPS coordinates");
			if (failure) failure();
		}, {
			timeout: 1000
		});
	};
	
	sdk.info.getVehicleConfiguration = function (callback) {
		gm.info.getVehicleConfiguration(function (config) {
			screenSize = config.screenSize;
			region = config.region;
			//Translate.set_language(config.language);
			if (callback) callback(config);
		}, function () {});
	};
	
	sdk.info.getVersionDisplayText = function (callback) {
		return gm.info.getVersionDisplayText();
	};
	
	sdk.info.getVIN = function () {
		return gm.info.getVIN();
	};
	
	sdk.info.watchButtons = function () {
		gm.info.watchButtons(function () {
			$(document).trigger("application.controls.pressed_back");
		}, ["BACK_SWITCH"]);
		gm.info.watchButtons(function () {
			$(document).trigger("application.controls.pressed_back");
		}, ["BTN_BACK"]);
	};
	
	sdk.info.watchRotaryControl = function () {
		gm.info.watchRotaryControl(function (status) {
			if (status == "RC_SELECT") {
				$(document).trigger('application.controls.pressed_select');
			}
			else if (status == "RC_CW") {
				$(document).trigger('application.controls.pressed_right');
			}
			else if (status == "RC_CCW") {
				$(document).trigger('application.controls.pressed_left');
			}
			else if (status == "RC_UP") {
				$(document).trigger('application.controls.pressed_up');
			}
			else if (status == "RC_DOWN") {
				$(document).trigger('application.controls.pressed_down');
			}
			else if (status == "RC_LEFT") {
				$(document).trigger('application.controls.pressed_left');
			}
			else if (status == "RC_RIGHT") {
				$(document).trigger('application.controls.pressed_right');
			}
		}, function (status) {
			log("ERROR: Got failure for gm.info.watchRotaryControl!");
		});
	};
	
	sdk.info.watchSteeringWheelControls = function () {
		gm.hmi.watchSteeringWheelControls(function (e) {
			if (e == "SWC_LEFT") {
				$(document).trigger("application.controls.pressed_left");
			}
			else if (e == "SWC_RIGHT") {
				$(document).trigger("application.controls.pressed_right");
			}
			else if (e == "SWC_UP") {
				$(document).trigger("application.controls.pressed_up");
			}
			else if (e == "SWC_DOWN") {
				$(document).trigger("application.controls.pressed_down");
			}
			else if (e == "SWC_SELECT") {
				$(document).trigger("application.controls.pressed_select");
			}
		});
	};
	
	sdk.info.getScreenSize = function () {
		return screenSize;
	};
	
	sdk.info.getRegion = function () {
		return region;
	};
	
	sdk.io.deleteFile = function (filename, isPrivate) {
		var opts = {};
		if (isPrivate) {
			opts.isPrivate = true;
		}
		gm.io.deleteFile(filename, opts);
	};
	
	sdk.io.readFile = function (filename, isPrivate, callback) {
		var opts = {};
		if (isPrivate) {
			opts.isPrivate = true;
		}
		/*
		var contents = gm.io.readFile(function (data) {
			log("readfile result: " + data);
			if (callback) { callback(data); }
		}, function () {
			// Failure
			log("Failure reading file " + filename + "!");
		}, filename, opts);
		*/
		var contents = gm.io.readFile(filename, opts);
		callback(contents);
	};
	
	sdk.io.readJSONFile = function (filename, isPrivate, callback) {
		sdk.io.readFile(filename, isPrivate, function (d) {
			if (typeof d == "undefined" || typeof d == "number" || d == null || d == "") {
				if (callback) { callback({}); }
			}
			else {
				if (callback) { callback(JSON.parse(decodeURIComponent(d))); }
			}
		});
	};
	
	sdk.io.writeFile = function (filename, contents, isPrivate) {
		var opts = {};
		if (isPrivate) {
			opts.isPrivate = true;
		}
		gm.io.writeFile(function () {
			log("Success writing file " + filename);
		}, function () {
			log("Error writing file " + filename);
		}, filename, contents, opts);
	};
	
	sdk.io.writeJSONFile = function (filename, contents, isPrivate) {
		var data = encodeURIComponent(JSON.stringify(contents));
		sdk.io.writeFile(filename, data, isPrivate);
	};
	
	/*sdk.media.play = function (url, callback) {
		sdk.media.stop();
		resetMediaTime();
		media.url = url;
		media.callback = callback;
		setTimeout(function () {
			log("Inside sdk media play, url: '" + url + "'");
			media.handle = gm.media.play(url, "exclusiveAudio", playerCallback);
		},1000);
	};
	
	sdk.media.pause = function () {
		log("Inside sdk media pause");
		if (media.handle) {
			gm.media.pause(media.handle);
		}
	};
	
	sdk.media.seek = function (position) {
		log("Inside sdk media seek, position: " + position);
		if (media.handle) {
			media.position = position;
			gm.media.seek(media.handle, position);
		}
	};
	
	sdk.media.stop = function () {
		log("Inside sdk media stop");
		if (media.handle) {
			gm.media.stop(media.handle);
			//gm.media.pause(media.handle);
			media.handle = null;
		}
		resetMediaTime();
	};
	
	sdk.media.getPosition = function () {
		compoundMediaTime();
		return media.position;
	};
	
	sdk.media.getStatus = function () {
		return media.status;
	};*/
	
	sdk.system.closeApp = function () {
		CCR.Media.stop();
		gm.system.closeApp(function () {
			log("ERROR: Failed to close app!");
		});
	};
	
	sdk.system.getSpeed = function () {
		return gm.system.getSpeed();
	};
	
	sdk.system.watchSpeed = function (callback) {
		gm.system.watchSpeed(callback, function () {});
	};
	
	sdk.system.setShutdown = function (callback) {
		gm.system.setShutdown(callback, function () {});
	};
	
	sdk.system.watchFocus = function (callback) {
		gm.system.watchFocus(callback, function () {});
	};
	
	sdk.ui.keyboard = function (id, onSubmit) {
    	var opts = {
    		sender: id,
    		language: "en-US",
    		kbType: 1,
    		feedbackMode: 0,
    		doneText: "Search",
    		onDone: onSubmit,
    		placeholder: "fsdfsdf"
    	};

    	var keyboard = new gm.widgets.Keyboard(opts);    	
    	return keyboard;
    };
	
	sdk.info.getVehicleConfiguration();
	
	sdk.info.watchRotaryControl();
	
	sdk.info.watchButtons();
	sdk.info.watchSteeringWheelControls();
})();